package com.kl.decodeandencode;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class MyMessageEncode extends MessageToByteEncoder<MyMessage> {

    @Override
    protected void encode(ChannelHandlerContext ctx, MyMessage msg, ByteBuf out) {
        System.out.println("===编码===");
        // 将消息的长度和内容编码写入缓冲区
        // 缓存区的格式为 int->byte
        // 底层也是将int类型的数据转换为byte
        out.writeInt(msg.getLength());
        out.writeBytes(msg.getMessage());
    }
}
